import axios from "axios";
import {ElMessage} from 'element-plus'
import router from "../router"
//
//请求拦截器
axios.interceptors.request.use(config => {
    let url = config.url;
    //给upload加入请求头Authorization
    if (url.split('/').pop() === 'upload'){
        let token = window.sessionStorage.getItem("token")
        if (token) {
            config.headers['Authorization'] = token;
        } else {
            ElMessage.error({message: "请先登录，将自动跳转"});
            router.replace('/login')
        }
    }
    return config;
}), error => {
    console.log(error)
}

//响应拦截器
axios.interceptors.response.use(success => {
    if (success.status && success.status === 200) {
        if (success.data.code === 500 || success.data.code === 403) {
            ElMessage.error({message: success.data.message});
            return;
        }
        if (success.data.code === 401) {
            ElMessage.error({message: success.data.message});
            router.push('/login')
        }

        if (success.data.message) {
            // ElMessage.success({message: success.data.message})
        }
    }
    return success.data;
}, error => {
    if (error.status === 504 || error.status === 404) {
        ElMessage.error({message: '服务器被吃掉了！'})
    } else if (error.status === 403) {
        ElMessage.error({message: "权限不足，请联系管理员"})
    } else if (error.status === 401) {
        ElMessage.error({message: '现在还没有登录呢'})
        router.replace('/')
    } else {
        if (error.message) {
            // alert(error.message)
            ElMessage.error({message: error.message})
        } else {
            ElMessage.error({message: '未知错误'})
        }
    }
    return;
})


let base = ''

//传送json格式的post请求
export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params
    })
}

//传送json的put请求
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${base}${url}`,
        data: params
    })
}
//传送json的get请求
export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: `${base}${url}`,
        data: params
    })
}
//传送json的delete请求
export const deleteRequest = (url, params) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`,
        data: params
    })
}


//removeCookie
export const delCookie = () => {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie =
            name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
    }
    if (cookies.length > 0) {
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            var domain = location.host.substr(location.host.indexOf("."));
            document.cookie =
                name +
                "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" +
                domain;
        }
    }
}

